﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Ele21RHelp.ViewMode
{
    public class 找不同Model : ObservableObject
    {

        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<找不同Item> _item1;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<找不同Item> item1 { get => _item1; set => SetProperty(ref _item1, value); }
        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<找不同Item> _item2;
        /// <summary>
        /// 
        /// </summary>
        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<找不同Item> _item3;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<找不同Item> item2 { get => _item2; set => SetProperty(ref _item2, value); }
        public ObservableCollection<找不同Item> item3 { get => _item3; set => SetProperty(ref _item3, value); }
        /// <summary>
        /// 
        /// </summary>
        private string _item2Str;
        /// <summary>
        /// 
        /// </summary>
        public string item2Str
        {
            get => _item2Str; set
            {
                SetProperty(ref _item2Str, value);
                item2 = GetItem(_item2Str);
                Const();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _item3Str;
        /// <summary>
        /// 
        /// </summary>
        public string item3Str
        {
            get => _item3Str; set
            {
                SetProperty(ref _item3Str, value);
                item3 = GetItem(_item3Str);
                Const();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _item1Str;
        /// <summary>
        /// 
        /// </summary>
        public string item1Str { get => _item1Str; set => SetProperty(ref _item1Str, value); }
        /// <summary>
        /// 
        /// </summary>
        private bool _isa;
        /// <summary>
        /// 
        /// </summary>
        public bool isa
        {
            get => _isa; set
            {
                SetProperty(ref _isa, value);
                SetClipboard();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private bool _isb;
        /// <summary>
        /// 
        /// </summary>
        public bool isb
        {
            get => _isb; set
            {
                SetProperty(ref _isb, value);
                SetClipboard();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private bool _isc;
        /// <summary>
        /// 
        /// </summary>
        public bool isc { get => _isc; set
            {
                SetProperty(ref _isc, value);
                SetClipboard();
            }
        }

        public ICommand AddCommand { get; set; }
        public ICommand DelCommand { get; set; }
        public ICommand ClearCommand { get; set; }
        List<string> strings = new List<string>() { "0", "沙", "椅", "钟", "灯", "壶", "镜" };
        public 找不同Model()
        {
            AddCommand = new RelayCommand<string>(AddCommandMethod);
            DelCommand = new RelayCommand(DelCommandMethod);
            ClearCommand = new RelayCommand(ClearCommandMethod);
            item1 = new ObservableCollection<找不同Item>();
            item2 = new ObservableCollection<找不同Item>();
            item3 = new ObservableCollection<找不同Item>();
        }

        private void ClearCommandMethod()
        {
            item1.Clear();
            Const();
            SetClipboard();
        }
        private void AddCommandMethod(string? obj)
        {
            if (item1.Count > 11)
            {
                return;
            }
            item1.Add(new 找不同Item()
            {
                showText = obj
            });
            Const();
            SetClipboard();
        }
        private void DelCommandMethod()
        {
            if (item1.Count == 0)
            {
                return;
            }
            item1.RemoveAt(item1.Count - 1);
            SetClipboard();
            Const();
        }
        private void SetClipboard()
        {
            string copystr = "";
            for (int i = 0; i < item1.Count; i++)
            {
                if (i == 4 || i == 8)
                {
                    copystr += "  ";
                }
                copystr += item1[i].showText;
            }
            string abc = "";
            if (isa)
            {
                abc += "红: ";
            }
            if (isb)
            {
                abc += "绿: ";
            }
            if (isc)
            {
                abc += "黄: ";
            }
            item1Str = abc+ copystr;

            HelpClass.CopyTo(item1Str);
        }
        private void Const()
        {
            for (int i = 0; i < 12; i++)
            {
                var value1 = GetItem(item1, i);
                var value2 = GetItem(item2, i);
                var value3 = GetItem(item3, i);
                if (value1.showText==value2.showText&& value1 .showText== value3.showText)
                {
                    value1.IsOk= true;
                    value2.IsOk= true;
                    value3.IsOk= true;
                }
                if (value1.showText == value2.showText && value1.showText != value3.showText)
                {
                    value1.IsOk = true;
                    value2.IsOk = true;
                    value3.IsOk = false;
                }
                if (value1.showText == value3.showText && value1.showText != value2.showText)
                {
                    value1.IsOk = true;
                    value2.IsOk = false;
                    value3.IsOk = true;
                }
                if (value2.showText == value3.showText && value1.showText != value2.showText)
                {
                    value1.IsOk = false;
                    value2.IsOk = true;
                    value3.IsOk = true;
                }
            }
        }
        private 找不同Item GetItem(ObservableCollection<找不同Item> item1, int i)
        {
            if (i < item1.Count)
            {
                return item1[i];
            }
            return new 找不同Item()
            {
                showText="x"
            };
        }

     

        private ObservableCollection<找不同Item> GetItem(string str)
        {
            ObservableCollection<找不同Item> dst = new ObservableCollection<找不同Item>();
            foreach (var item in str)
            {
                if (strings.Contains(item.ToString()))
                {
                    dst.Add(new 找不同Item()
                    {
                        showText = item.ToString(),
                    });
                }
            }
            return dst;
        }
    }
    public class 找不同Item : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        private string _showText;
        /// <summary>
        /// 
        /// </summary>
        public string showText { get => _showText; set => SetProperty(ref _showText, value); }

        /// <summary>
        /// 
        /// </summary>
        private bool _IsOk;
        /// <summary>
        /// 
        /// </summary>
        public bool IsOk { get => _IsOk; set => SetProperty(ref _IsOk, value); }

    }
}
